package r25.tools;

import java.util.Map;

/**
 * <p>ParameterHelper2 class.</p>
 *
 * @author u211354
 * @version $Id: $
 */
public class ParameterHelper2 {
    /**
     * <p>newInstance</p>
     *
     * @param str a {@link java.lang.String} object.
     * @return a T object.
     * @throws java.lang.Exception if any.
     */
    public static <T extends IParametrable> T newInstance(String str)
            throws Exception {
        Map<String,Object> map = JsonHelper.loadJson(str);
        return newInstance(map);
    }

    /**
     * <p>newInstance</p>
     *
     * @param map a {@link java.util.Map} object.
     * @return a T object.
     * @throws java.lang.Exception if any.
     */
    @SuppressWarnings("unchecked")
    public static <T extends IParametrable> T newInstance(Map<String,Object> map)
            throws Exception {
        String className = (String)map.get("className");
        if (className != null) {
            T t = (T)Class.forName(className).newInstance();
            t.parameter(map);
            return t;
        }
        return null;
    }

    /**
     * <p>newInstance</p>
     *
     * @param cl a {@link java.lang.ClassLoader} object.
     * @param str a {@link java.lang.String} object.
     * @return a T object.
     * @throws java.lang.Exception if any.
     */
    public static <T extends IParametrable> T newInstance(ClassLoader cl,
            String str) throws Exception {
        Map<String,Object> map = JsonHelper.loadJson(str);
        return newInstance(cl, map);
    }

    /**
     * <p>newInstance</p>
     *
     * @param cl a {@link java.lang.ClassLoader} object.
     * @param map a {@link java.util.Map} object.
     * @return a T object.
     * @throws java.lang.Exception if any.
     */
    public static <T extends IParametrable> T newInstance(ClassLoader cl,
            Map<String,Object> map) throws Exception {
        String className = (String)map.get("className");
        if (className != null) {
            @SuppressWarnings("unchecked")
            T t = (T)cl.loadClass(className).newInstance();
            t.parameter(map);
            return t;
        }
        return null;
    }
}
